package com.lzy.system.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzy.system.business.service.ISysBusEntityFieldService;
import com.lzy.system.business.dto.convert.SysBusEntityFieldConvert;
import com.lzy.system.business.entity.SysBusEntityField;
import com.lzy.system.business.dto.SysBusEntityFieldAddDTO;
import com.lzy.system.business.dto.SysBusEntityFieldUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.conf.JSONResult;

import java.util.List;
import javax.validation.constraints.NotBlank;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 存储字段 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2024-01-21
 */
@RestController
@RequestMapping("/business/sys-bus-entity-field")
@Api(tags = "存储字段")
public class SysBusEntityFieldController {
    private ISysBusEntityFieldService iSysBusEntityFieldService;
    private SysBusEntityFieldConvert sysBusEntityFieldConvert;

    @Autowired
    public SysBusEntityFieldController(ISysBusEntityFieldService iSysBusEntityFieldService, SysBusEntityFieldConvert sysBusEntityFieldConvert) {
        this.iSysBusEntityFieldService = iSysBusEntityFieldService;
        this.sysBusEntityFieldConvert = sysBusEntityFieldConvert;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ApiOperation("获取所有存储字段列表 ")
    public JSONResult getList(
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) {
        IPage<SysBusEntityField> page_obj = iSysBusEntityFieldService.page(new Page<SysBusEntityField>(page, limit));
        return JSONResult.ok(page_obj);
    }

    @RequestMapping(value = "/getListByEntity_id", method = RequestMethod.POST)
    @ApiOperation("根据存储id获取存储字段列表")
    public JSONResult getListByEntity_id(
            @ApiParam("存储id") @RequestParam(name = "entity_id") String entity_id
    ) {

        return JSONResult.ok(iSysBusEntityFieldService.list(
                new QueryWrapper<SysBusEntityField>().eq("entity_id", entity_id).orderByAsc("order_index")
        ));
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加存储字段")
    public JSONResult add(
            @ApiParam("存储字段") @RequestBody @Validated SysBusEntityFieldAddDTO data
    ) {
        SysBusEntityField save_data = sysBusEntityFieldConvert.addDtoToEntity(data);
        iSysBusEntityFieldService.save(save_data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改存储字段")
    public JSONResult update(
            @ApiParam("存储字段") @RequestBody @Validated SysBusEntityFieldUpdateDTO data
    ) {
        SysBusEntityField save_data = sysBusEntityFieldConvert.updateDtoToEntity(data);
        iSysBusEntityFieldService.updateById(save_data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除存储字段")
    public JSONResult delete(
            @ApiParam("ids") @RequestBody @Validated List<String> ids
    ) {
        iSysBusEntityFieldService.removeByIds(ids);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation("获取存储字段详情")
    public JSONResult get(
            @ApiParam("id") @PathVariable(value = "id") @NotBlank(message = "id不能为空") String id
    ) {
        return JSONResult.ok(iSysBusEntityFieldService.getById(id));
    }
}
